package com.clementheliou.cheliou.business.dataaccess.comment.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.clementheliou.cheliou.business.dataaccess.comment.CommentDataAccessService;
import com.clementheliou.cheliou.dal.dao.comment.CommentDAO;
import com.clementheliou.cheliou.dal.model.comment.Comment;

/**
 * {@link CommentDataAccessService} that used the default implementation
 * provided by the model's layer. It should not be used if many implementations
 * are availables in this last one.
 * <p>
 * Due to the {@link Transactional} annotation and the value of its
 * <i>readOnly</i> attribute, all methods are called in a transaction that will
 * be rollbacked in the end. This behavior can be overriden at the method level,
 * more particularly if you need to perform an insert, update or delete action.
 * </p>
 * 
 * @author Clément HELIOU (clement.heliou@gmail.com)
 * @see {@link CommentDataAccessService};
 * @see {@link Service};
 * @see {@link Transactional}.
 * @since 1.0
 */
@Service
@Transactional(readOnly = true)
public class CommentDefaultDataAccessService implements CommentDataAccessService {

	/**
	 * Logger for this class.
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @see {@link Logger};
	 * @see {@link LoggerFactory}.
	 * @since 1.0
	 */
	private static final Logger LOGGER = LoggerFactory.getLogger(CommentDefaultDataAccessService.class);

	/**
	 * DAO used to access the entities of {@link Comment} type.
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @see {@link Comment}.
	 * @since 1.0
	 */
	@Autowired
	private CommentDAO commentDAO;

	/**
	 * <i>{@inheritDoc}</i>
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @see {@link Transactional}.
	 * @since 1.0
	 */
	@Override
	@Transactional(readOnly = false)
	public void addComment(final Comment commentToBeAdded) {
		LOGGER.debug("Add the comment of the member {} for the photo.", commentToBeAdded.getAuthor().getId(), commentToBeAdded.getCommentedPhoto().getId());
		commentDAO.saveEntity(commentToBeAdded);
	}

	/**
	 * <i>{@inheritDoc}</i>
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @since 1.0
	 */
	@Override
	public Integer countCommentsOfPhoto(Integer photoId) {
		LOGGER.debug("Count the comments of the photo {}.", photoId);
		return commentDAO.countCommentsOfPhoto(photoId);
	}

	/**
	 * <i>{@inheritDoc}</i>
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @since 1.0
	 */
	@Override
	public List<Comment> getCommentsOfPhoto(Integer photoId, int first, int pageSize) {
		LOGGER.debug("Get the comments of the photo {} from the {} with the page size {}.", new Object[] { photoId, first, pageSize });
		return commentDAO.getCommentsOfPhoto(photoId, first, pageSize);
	}

}
